package com.pdg.android.russianroulette;

import java.util.Random;

import com.pdg.android.common.GameDataBase;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class GameData extends GameDataBase<GameState>{
	private static final String KEY_GAMES_WON = "games_won";
	private static final int DEFAULT_GAMES_WON = 0;
	private static final String KEY_GAMES_LOST = "games_lost";
	private static final int DEFAULT_GAMES_LOST = 0;
	private static final String KEY_TOTAL_SCORE_WON = "total_score_won";
	private static final int DEFAULT_TOTAL_SCORE_WON = 0;
	private static final String KEY_TOTAL_SCORE_LOST = "total_score_lost";
	private static final int DEFAULT_TOTAL_SCORE_LOST = 0;
	private static final String KEY_HIGH_SCORE_WON = "high_score_won";
	private static final int DEFAULT_HIGH_SCORE_WON = 0;
	private static final String KEY_HIGH_SCORE_LOST = "high_score_lost";
	private static final int DEFAULT_HIGH_SCORE_LOST = 0;
	private static final String KEY_CURRENT_SCORE = "current_score";
	private static final int DEFAULT_CURRENT_SCORE = 0;
	private static final int MAXIMUM_MOVE = 6;
	private static final int MINIMUM_MOVE = 1;
	private static final int SCORE_PER_ROUND = 1;
	private static final int ONE = 1;
	private static final String KEY_LAST_ROUND_MESSAGE = "last_round_message";
	private static final String DEFAULT_LAST_ROUND_MESSAGE="";
	
	private int gamesWon;
	private int gamesLost;
	private int totalScoreWon;
	private int totalScoreLost;
	private int highScoreWon;
	private int highScoreLost;
	private int currentScore;
	private Random random = new Random();
	private String lastRoundMessage;
	
	public String getLastRoundMessage() {
		return lastRoundMessage;
	}
	public int getCurrentScore() {
		return currentScore;
	}
	public int getCurrentRound(){
		return getCurrentScore()+ONE;
	}
	public int getTotalScoreWon() {
		return totalScoreWon;
	}
	public int getAverageScoreWon(){
		if(getGamesWon()==0) return 0;
		return getTotalScoreWon()/getGamesWon();
	}
	public int getAverageScoreLost(){
		if(getGamesLost()==0) return 0;
		return getTotalScoreLost()/getGamesLost();
	}
	public int getTotalScoreLost() {
		return totalScoreLost;
	}
	public int getHighScoreWon() {
		return highScoreWon;
	}
	public int getHighScoreLost() {
		return highScoreLost;
	}
	public int getGamesWon() {
		return gamesWon;
	}
	public int getGamesLost() {
		return gamesLost;
	}
	public int getGamesPlayed(){
		return getGamesWon()+getGamesLost();
	}
	public void setLastRoundMessage(String theValue) {
		lastRoundMessage = theValue;
	}
	public void setCurrentScore(int theValue) {
		currentScore = theValue;
	}
	public void setHighScoreLost(int theValue) {
		highScoreLost = theValue;
	}
	public void setHighScoreWon(int theValue) {
		highScoreWon = theValue;
	}
	public void setTotalScoreLost(int theValue) {
		totalScoreLost = theValue;
	}
	public void setTotalScoreWon(int theValue) {
		totalScoreWon = theValue;
	}
	public void setGamesLost(int theValue) {
		gamesLost = theValue;
	}
	public void setGamesWon(int theValue) {
		gamesWon = theValue;
	}
	public void clearStatistics(){
		setGamesWon(0);
		setGamesLost(0);
		setHighScoreLost(0);
		setHighScoreWon(0);
		setTotalScoreWon(0);
		setTotalScoreLost(0);
	}
	public void recordWinningGame(int theScore){
		setTotalScoreWon(getTotalScoreWon()+theScore);
		if(theScore>getHighScoreWon()){
			setHighScoreWon(theScore);
		}
		setGamesWon(getGamesWon()+ONE);
	}
	public void recordLosingGame(int theScore){
		setTotalScoreLost(getTotalScoreLost()+theScore);
		if(theScore>getHighScoreLost()){
			setHighScoreLost(theScore);
		}
		setGamesLost(getGamesWon()+ONE);
	}
	public MoveResults makeMove(String theMoveString){
		int theMove;
		try{
			theMove = Integer.parseInt(theMoveString);
		}catch(Exception e){
			e.printStackTrace();
			return MoveResults.INVALID;
		}
		if(theMove>MAXIMUM_MOVE || theMove<MINIMUM_MOVE){
			return MoveResults.OUT_OF_RANGE;
		}
		int theChamber = random.nextInt(MAXIMUM_MOVE-MINIMUM_MOVE+ONE)+MINIMUM_MOVE;
		if(theChamber==theMove){
			return MoveResults.DIE;
		}else{
			setCurrentScore(getCurrentScore()+SCORE_PER_ROUND);
			return MoveResults.LIVE;
		}
	}
	public void newGame(){
		setCurrentScore(DEFAULT_CURRENT_SCORE);
		setLastRoundMessage(DEFAULT_LAST_ROUND_MESSAGE);
	}
	public GameData(){
	}
	@Override
	protected void onLoad(SharedPreferences thePreferences) {
		setGamesWon(thePreferences.getInt(KEY_GAMES_WON, DEFAULT_GAMES_WON));
		setGamesLost(thePreferences.getInt(KEY_GAMES_LOST, DEFAULT_GAMES_LOST));
		setTotalScoreWon(thePreferences.getInt(KEY_TOTAL_SCORE_WON, DEFAULT_TOTAL_SCORE_WON));
		setTotalScoreLost(thePreferences.getInt(KEY_TOTAL_SCORE_LOST, DEFAULT_TOTAL_SCORE_LOST));
		setHighScoreWon(thePreferences.getInt(KEY_HIGH_SCORE_WON, DEFAULT_HIGH_SCORE_WON));
		setHighScoreLost(thePreferences.getInt(KEY_HIGH_SCORE_LOST, DEFAULT_HIGH_SCORE_LOST));
		setCurrentScore(thePreferences.getInt(KEY_CURRENT_SCORE, DEFAULT_CURRENT_SCORE));
		setLastRoundMessage(thePreferences.getString(KEY_LAST_ROUND_MESSAGE, DEFAULT_LAST_ROUND_MESSAGE));
	}
	@Override
	protected GameState getDefaultCurrentState() {
		return GameState.MAIN_MENU;
	}
	@Override
	protected GameState decodeCurrentState(String theValue) {
		return GameState.valueOf(theValue);
	}
	protected String encodeCurrentState(GameState theValue){
		return theValue.name();
	}
	@Override
	protected void onCommit(Editor theEditor) {
		theEditor.putInt(KEY_GAMES_LOST, getGamesLost());
		theEditor.putInt(KEY_GAMES_WON, getGamesWon());
		theEditor.putInt(KEY_HIGH_SCORE_LOST, getHighScoreLost());
		theEditor.putInt(KEY_HIGH_SCORE_WON, getHighScoreWon());
		theEditor.putInt(KEY_TOTAL_SCORE_LOST, getTotalScoreLost());
		theEditor.putInt(KEY_TOTAL_SCORE_WON, getTotalScoreWon());
		theEditor.putInt(KEY_CURRENT_SCORE, getCurrentScore());
		theEditor.putString(KEY_LAST_ROUND_MESSAGE, getLastRoundMessage());
	}
}
